package com.can.blockingQueue;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class SynchronousQueueDemo02 {

    public static void main(String[] args) {
        SynchronousQueue synchronousQueue = new SynchronousQueue();
        AtomicInteger i = new AtomicInteger(1);
        new Thread(()->{
            while (true){
                try {
                    TimeUnit.SECONDS.sleep(1);
                    System.out.println("开始put");
                    synchronousQueue.put(i.getAndIncrement()); //当另一个线程 kate获取后 才会继续执行下面的代码
                    System.out.println("put阻塞结束");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        new Thread(()->{
            while (true){
                try {
                    TimeUnit.SECONDS.sleep(6);
                    System.out.println("开始kate");
                    System.out.println("kate得到的值=>"+synchronousQueue.take());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
